package com.coocaa.app.lib.cleaner.mgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lu on 17-4-11.
 */

public interface DataHolder extends DataLoader {
    class XXRubbish<T> implements Cloneable {
        public final T clone() {
            T o = null;
            try {
                o = (T) super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return o;
        }

        public void reset() {

        }
    }

    class ApkRubbish extends XXRubbish<ApkRubbish> {
        public Map<String, Long> apks = new HashMap<String, Long>();
        public long size;

        @Override
        public void reset() {
            synchronized (this) {
                apks.clear();
                size = 0;
            }
        }
    }

    class SinglePackageCache extends XXRubbish<SinglePackageCache> {
        public String packageName;
        public long size;

        @Override
        public boolean equals(Object o) {
            try {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;

                SinglePackageCache cache = (SinglePackageCache) o;

                return packageName != null ? packageName.equals(cache.packageName) : cache.packageName == null;
            } catch (Exception e) {
                return false;
            }
        }

        @Override
        public int hashCode() {
            return packageName != null ? packageName.hashCode() : 0;
        }
    }

    class PackageCache extends XXRubbish<PackageCache> {
        public List<SinglePackageCache> caches = new ArrayList<SinglePackageCache>();
        public long size;

        @Override
        public void reset() {
            synchronized (this) {
                caches.clear();
                size = 0;
            }
        }

        public void remove(SinglePackageCache cache) {
            for (SinglePackageCache c : caches) {
                if (c.equals(cache)) {
                    caches.remove(c);
                    return;
                }
            }
        }

        public boolean contains(SinglePackageCache cache) {
            for (SinglePackageCache c : caches) {
                if (c.equals(cache)) {
                    return true;
                }
            }
            return false;
        }
    }

    class SinglePackageData extends XXRubbish<SinglePackageData> {
        public String packageName;
        public List<String> files = new ArrayList<String>();
        public long size;

        @Override
        public void reset() {
            synchronized (this) {
                size = 0;
                if (files != null)
                    files.clear();
            }
        }

        @Override
        public boolean equals(Object o) {
            try {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
                SinglePackageData data = (SinglePackageData) o;
                return packageName != null ? packageName.equals(data.packageName) : data.packageName == null;
            } catch (Exception e) {
                return false;
            }
        }

        @Override
        public int hashCode() {
            return packageName != null ? packageName.hashCode() : 0;
        }
    }

    class PackageData extends XXRubbish<PackageData> {
        public List<SinglePackageData> datas = new ArrayList<SinglePackageData>();
        public long size;

        @Override
        public void reset() {
            synchronized (this) {
                datas.clear();
                size = 0;
            }
        }

        public void remove(SinglePackageData data) {
            for (SinglePackageData d : datas) {
                if (d.equals(data)) {
                    datas.remove(d);
                    return;
                }
            }
        }

        public boolean contains(SinglePackageData data) {
            for (SinglePackageData d : datas) {
                if (d.equals(data))
                    return true;
            }
            return false;
        }
    }

    class OtherData extends XXRubbish<OtherData> {
        public Map<String, Long> datas = new HashMap<String, Long>();
        public long size;

        @Override
        public void reset() {
            synchronized (this) {
                datas.clear();
                size = 0;
            }
        }
    }

    interface DataHolderListener {
        void onApkRubbishUpdate(ApkRubbish apkRubbish, String apkFile);

        void onPackageCacheUpdate(PackageCache packageCache, String packageName);

        void onPackageDataUpdate(PackageData packageData, String packageName);

        void onOtherDataUpdate(OtherData otherData, String file);
    }


    /**
     * 所有记录的垃圾信息清空，准备重新增加
     */
    void reset(DataHolderListener listener);

    /**
     * 添加安装包垃圾
     *
     * @param apk 安装包文件绝对路径
     */
    void appendAPK(String apk);

    void removeAPK(String apk);

    /**
     * 增加应用缓存垃圾
     */
    void appendPackageCache(SinglePackageCache data);

    void removePackageCache(SinglePackageCache cache);

    /**
     * 增加应用数据垃圾
     */
    void appendPackageData(SinglePackageData data);

    void removePackageData(SinglePackageData data);

    /**
     * 增加其他垃圾
     *
     * @param file
     */
    void appendOtherData(String file);

    void removeOtherData(String file);
}
